//
//  SISSAddPassengerTakeLookViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/9/4.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSAddPassengerTakeLookViewController.h"
#import "SISSHouseResourceCooperationViewController.h"
#import "SISSPassengerTakeLookRecordViewController.h"
#import "SISSNetworkingManager+PassengerProgress.h"
#import "SISSPassengerProgressModel.h"

@interface SISSAddPassengerTakeLookViewController ()

/* 房源模型 */
@property (strong, nonatomic) SISSHouseResourceModel *houseModel;

@end

@implementation SISSAddPassengerTakeLookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.chooseView.leftStr = @"*房源信息";
    self.chooseView.rightPlaceStr = @"请选择房源信息";
}

- (void)chooseViewChooseAnction {
    
    SISSHouseResourceCooperationViewController *vc = [[SISSHouseResourceCooperationViewController alloc] init];
    vc.isProgressEnter = YES;
    vc.title = @"房源";
    vc.completeBlock = ^(SISSHouseResourceModel *houseModel) {
        self.chooseView.rightStr = @"已选择";
        self.houseModel = houseModel;
        
        
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)confirmButtonClickAction:(UIButton *)sender {
    
    [super confirmButtonClickAction:sender];
    
    if (!self.dateStr) {
        [SISSTools showSVTitle:@"请选择看房时间" andTime:1.0 andColor:YES];
        return;
    }
    if (self.dateStr.length > 0 && [PublicUtil compareOneDate:[NSDate date] withAnotherDate:[PublicUtil getNSDateFromSimpleDateString:self.dateStr]] == NSOrderedAscending) {
        [self alertWithMessage:@"看房时间不能大于当前时间"];
        return ;
    }
    if (!self.houseModel) {
        [SISSTools showSVTitle:@"请选择房源信息" andTime:1.0 andColor:YES];
        return;
    }
    if (self.images.count == 0) {
        [SISSTools showSVTitle:@"请选择看房照片" andTime:1.0 andColor:YES];
        return;
    }
    
//    int ahrId = self.houseModel.ID ? (int)self.houseModel.ID : -1;
    NSString *timeStr = [self.dateStr stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    NSString *ahrId = self.houseModel.houseresType == 2 ? self.houseModel.ID : self.houseModel.ahbpId;
    NSString *ahbpId = self.houseModel.houseresType == 1 ? self.houseModel.ahbpId : @"";
    NSDictionary *parameters = @{
                                 @"adbpId":self.passengerProgressModel.adbpId,
                                 @"type":@(self.houseModel.houseresType),
                                 @"ahrId":ahrId,
                                 @"ahbpId":ahbpId,
                                 @"mDate":timeStr 
                                 };
    
    [kNetworkNoUTF8Manager insertPassengerProgressTakeLookWithParameters:parameters images:self.images success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] != 200) {
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:responseObject[@"message"] andTime:1.0 andColor:YES];
            }];
            return ;
        }
        
        if ([responseObject[@"code"] integerValue] == 200) { // 添加成功
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:@"添加成功，等待对方确认。" andTime:1.0 andColor:YES];
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
